//
//  GZConsoleLogFormat.m
//  GZLogger
//
//  Created by GR on 2018/12/20.
//  Copyright © 2018 GR. All rights reserved.
//

#import "GZConsoleLogFormatter.h"
#import "GZLogItem.h"

@implementation GZConsoleLogFormatter

#pragma mark - format text segment

- (NSString *)dateTextWithItem:(GZLogItem *)item {
    
    static NSDateFormatter *formatter;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        formatter               = [NSDateFormatter new];
        formatter.dateFormat    = @"yyyy/MM/dd HH:mm:ss.SSS";
        formatter.calendar      = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    });
    
    return [formatter stringFromDate:item.time];
}

- (NSString *)levelTextWithItem:(GZLogItem *)item {
    return item.levelText;
}

- (NSString *)tagTextWithItem:(GZLogItem *)item {
    return item.tag;
}

- (NSString *)traceTextWithItem:(GZLogItem *)item {
    return [item.function stringByAppendingFormat:@"- %04d -", (int)item.line];
}

- (NSString *)contentTextWithItem:(GZLogItem *)item {
    
    NSString *text = item.content;
    
    if (item.stackLevel > 0) {
        text = [text stringByAppendingFormat:@"\nStackSymbols are: \n%@\n", item.stackSymbols];
    }
    
    return text;
}

#pragma mark - GZConsoleLogFormatter

- (NSString *)formattedTextWithItem:(GZLogItem *)item {
    
    NSString *text = @"";
    
    text = [text stringByAppendingString:[self dateTextWithItem:item]];
    text = [text stringByAppendingString:@"|"];
    text = [text stringByAppendingString:[self levelTextWithItem:item]];
    text = [text stringByAppendingString:@"|"];
    text = [text stringByAppendingString:[self tagTextWithItem:item]];
    text = [text stringByAppendingString:@"|"];
    text = [text stringByAppendingString:[self traceTextWithItem:item]];
    text = [text stringByAppendingString:@"|"];
    text = [text stringByAppendingString:[self contentTextWithItem:item]];
    text = [text stringByAppendingString:@"\n"];
    
    return text;
}
@end
